What is @ethersproject/bytes?
@ethersproject/bytes is a utility library for handling and manipulating byte arrays in JavaScript. It is part of the ethers.js library, which is widely used for interacting with the Ethereum blockchain. This package provides various functions to work with byte arrays, including conversion, concatenation, slicing, and more.
What are @ethersproject/bytes's main functionalities?
Conversion between hex strings and byte arrays
This feature allows you to convert a hex string to a byte array and vice versa. The `arrayify` function converts a hex string to a Uint8Array, and the `hexlify` function converts a Uint8Array back to a hex string.
const { arrayify, hexlify } = require('@ethersproject/bytes');
const hexString = '0x123456';
const byteArray = arrayify(hexString);
console.log(byteArray); // Uint8Array [ 18, 52, 86 ]
const newHexString = hexlify(byteArray);
console.log(newHexString); // '0x123456'
Concatenation of byte arrays
This feature allows you to concatenate multiple byte arrays into a single byte array. The `concat` function takes an array of Uint8Arrays and returns a new Uint8Array that is the concatenation of all the input arrays.
const { concat } = require('@ethersproject/bytes');
const byteArray1 = new Uint8Array([1, 2, 3]);
const byteArray2 = new Uint8Array([4, 5, 6]);
const concatenatedArray = concat([byteArray1, byteArray2]);
console.log(concatenatedArray); // Uint8Array [ 1, 2, 3, 4, 5, 6 ]
Slicing byte arrays
This feature allows you to slice a byte array to get a subarray. The `slice` function takes a byte array and start and end indices, and returns a new byte array that is a subarray of the input array.
const { arrayify, hexlify, slice } = require('@ethersproject/bytes');
const hexString = '0x1234567890abcdef';
const byteArray = arrayify(hexString);
const slicedArray = slice(byteArray, 2, 6);
console.log(hexlify(slicedArray)); // '0x34567890'
Other packages similar to @ethersproject/bytes
buffer
The 'buffer' package is a core Node.js module that provides a way to handle binary data. It offers similar functionalities to @ethersproject/bytes, such as conversion between different formats, concatenation, and slicing of byte arrays. However, 'buffer' is more general-purpose and not specifically tailored for Ethereum or blockchain-related tasks.
ethereumjs-util
The 'ethereumjs-util' package is a collection of utility functions for Ethereum, including functions for handling byte arrays. It provides similar functionalities to @ethersproject/bytes, such as conversion between hex strings and byte arrays, and byte array manipulation. However, 'ethereumjs-util' includes a broader range of utilities specifically for Ethereum development.
Byte Manipulation
This sub-module is part of the ethers project.
It is responsible for manipulating binary data.
For more information, see the documentation.
Importing
Most users will prefer to use the umbrella package,
but for those with more specific needs, individual components can be imported.
const {
isBytesLike,
isBytes,
arrayify,
concat,
stripZeros,
zeroPad,
isHexString,
hexlify,
hexDataLength,
hexDataSlice,
hexConcat,
hexValue,
hexStripZeros,
hexZeroPad,
splitSignature,
joinSignature,
Bytes,
BytesLike,
DataOptions,
Hexable,
SignatureLike,
Signature
} = require("@ethersproject/bytes");
License
MIT License